<template>
	<view class="mainpadding2">
		<view class="mainpadding_top ffffff radius">
			<view class="flexbetween">
				<view class="titletext bold hei_text">评价</view>
				<view class="flexright">
					<view class="flexleft margin_right" @click="pjtype=5">
						<view class="margin_right1" :class="pjtype==5?'haopings':'haoping'"></view>
						<view :class="pjtype==5?'xiaolan':'xiaohui'">好评</view>
					</view>
					<view class="flexleft margin_right" @click="pjtype=3">
						<view class="margin_right1" :class="pjtype==3?'zhongpings':'zhongping'"></view>
						<view :class="pjtype==3?'xiaolan':'xiaohui'">中评</view>
					</view>
					<view class="flexleft" @click="pjtype=1">
						<view class="margin_right1" :class="pjtype==1?'chapings':'chaping'"></view>
						<view :class="pjtype==1?'xiaolan':'xiaohui'">差评</view>
					</view>
				</view>
			</view>
			<textarea class="textareaclass margin_top main_size hei_text bold" placeholder="请输入评价内容~"
				v-model="content"></textarea>
		</view>
		<view class="mainpadding_top ffffff radius" v-if="type!=5">
			<view class="titletext hei_text bold">上传图片</view>
			<view class="flexleft">
				<view class="itemimgBox flexcenter margin_top" v-for="(item,index) in images_text" :key="index">
					<view class="listleftimg">
						<image class="listleftimg radius" :src="item" mode="">
						</image>
						<view class="deleteimg" @click="delindex(index,images,images_text)"></view>
					</view>
				</view>
				<view class="itemimgBox flexcenter margin_top" v-if="images.length<4"
					@click="uploadImg(4,images,images_text)">
					<image class="listleftimg radius" src="@/static/image/system/upimg.png" mode=""></image>
				</view>
			</view>
		</view>
		<view class="posibottom mainpadding">
			<view class="submitbtn" @click="submit">发表评价</view>
		</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")

	export default {
		data() {
			return {
				pjtype: "5", //1=差评，3=中评，5=好评
				listid: "",
				type: 1, //1优课,2辅导,3测试,4资料，5回答：评价
				content: "",
				images: [],
				images_text: [],
			}
		},
		onLoad(options) {
			this.type = options.type
			this.listid = options.id
		},
		methods: {
			submit() {
				if (this.content == "") {
					httpRequest.toast("请输入评价内容！")
					return false
				}
				let url = ""
				let data = {}
				if (this.type == 5) {
					url = "/api/teacher/comment"
					data = {
						teacher_question_id: this.listid,
						composite_evaluate: this.pjtype,
						content: this.content
					}
				} else {//1优课,2辅导,3测试,4资料 评价
					url = "/api/lesson/comment"
					data = {
						order_id: this.listid,
						composite_evaluate: this.pjtype,
						content: this.content,
						images: this.images.join()
					}
				}
				httpRequest.request(url, 'POST', data).then(res => {
					httpRequest.toast(res.msg)
					if (res.code == 1) {
						setTimeout(() => {
							uni.navigateBack(1)
						}, 1000)
					}
				})
			},
			uploadImg(value, upImgs, upImgs_text) { //上传图片
				let _this = this;
				let num = Number(value) - upImgs.length;
				if (num <= 0) {
					return false;
				}
				uni.showActionSheet({
					itemList: ['相机', '相册'],
					success: function(res) {
						// 选择相机走0
						if (res.tapIndex == 0) {
							httpRequest.chooseImage(num, ['camera']).then(res => {
								for (var i = 0; i < res.length; i++) {
									_this.uploadImageService(res[i], upImgs, upImgs_text)
								}
							}).catch(err => {});
						}
						// 选择相册走1
						if (res.tapIndex == 1) {
							httpRequest.chooseImage(num, ['album']).then(res => {
								for (var i = 0; i < res.length; i++) {
									_this.uploadImageService(res[i], upImgs, upImgs_text)
								}
							}).catch(err => {});
						}
					},
					fail: function(res) {}
				});
			},
			delindex(index,val1,val2){
				val1.splice(index,1)
				val2.splice(index,1)
			},
			uploadImageService(url, upImg, upImgs_text) {
				let _this = this;
				httpRequest.uploadFile('/api/common/upload', url).then(res => {
					let image_item = res.fullurl;
					upImg.push(res.url)
					upImgs_text.push(image_item);
				}).catch(err => {});
			},
		}
	}
</script>

<style>

</style>